//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class ColorMappingOverride: IOfficeXmlSerializable
    {
        public void ReadXml(ReadContext context)
        {
            using (var c = context.ReadAndSwitch("clrMapOvr", DrawingConstants.DrawingNamespaceUri, false))
            {
                switch (c.ReaderLocalName)
                {
                    case "masterClrMapping":
                        ColorMapping = null;
                        using (var c2 = c.Read(c.ReaderLocalName))
                        {}
                        break;
                    case "overrideClrMapping":
                        this.ColorMapping = c.ReadElementAs<ColorMapping>("overrideClrMapping");
                        break;
                    default: throw new NotSupportedException();
                }
            }
        }

        public ColorMapping ColorMapping { get; set; }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.WriteAndSwitch("clrMapOvr", DrawingConstants.DrawingNamespace))
            {
                if (ColorMapping != null)
                {
                    c.WriteElementAs(ColorMapping, "overrideClrMapping");
                }
                else
                {
                    using (c.Write("masterClrMapping"))
                    {}
                }
            }
        }
    }
}